<?php
 class Core_Business_Training extends Core_Business_Abstract{
	private $_id;
	private $_name;
	private $_detail;
	private $_created;
	private $_updated;
	private $_createdby;
	private $_category;
	private $_paymentid;
	private $_zone;
	
	public function getId() {return $this->_id;}
	public function setId($value) {
		//if(is_integer($value))
			$this->_id = $value;
		//else
			//throw new Exception("Property value for object ".__CLASS__.", property ".__METHOD__."must be an integer, value".$value);
	}
	
	public function getName() {return $this->_name;}
	public function setName($value) {
		if ($value)
			$this->_name = $value;
		else 
			throw new Exception("Property value for object ".__CLASS__.", property ".__METHOD__);
	}
	public function getDetail() {return $this->_detail;}
	public function setDetail($value) {
		if($value)
			$this->_detail = $value;
		else 
			throw new Exception("Property value for object ".__CLASS__.", property ".__METHOD__);
	}
	public function getCreated() {return $this->_created;}
	public function setCreated($value) {$this->_created=$value;}
	public function getUpdated() {return $this->_updated;}
	public function setUpdated($value) {$this->_updated=$value;}
	
	public function getCreatedBy() {return $this->_createdby;}
	public function setCreatedBy($value) {$this->_createdby = $value;}
	
	public function getCategoryid() {
		return $this->_category;
	}
	
	public function getPaymentid(){ return $this->_paymentid;}
 	public function getPayment(){ 
 		if(isset($this->_paymentid))
 			return Core_Business_Payment::getById($this->_paymentid);
 		else 
 			return null;
 	}
 	public function setPaymentid($value){ $this->_paymentid = $value;}
	
	/***
	 * return the owner user object of the training
	 * @param
	 * @return Core_Business_User|null
	 */
	public function getAuthor() {
		return Core_Business_User::getById($this->_createdby);
		
	}
	
	public function getExpertises() {
		$map = new Core_Data_Mappers_Trainingexpertise();
		$category = $map->find($this->_id);
		
		return $category;
	}
	
	public function setExpertises($category) {
		if ($this->id) {
			$map = new Core_Data_Mappers_Trainingexpertise();
			$map->flushCategory($this->_id);
			if(is_array($category)) {
				foreach ($category as $id) {
					if($id instanceof Core_Business_Trainingexpertise) {
						$id->setTrainingid($this->_id);
						$map->save($id);
					}
					else {
						$lkcategory = new Core_Business_Trainingexpertise();
						$lkcategory->setExpertiseid($id);
						$lkcategory->setTrainingid($this->Id);
						$map->save($lkcategory);
					}		
				}
			}
			else {
				$lkcategory = new Core_Business_Trainingexpertise();
				$lkcategory->setExpertiseid($category->Id);
				$lkcategory->setTrainingid($this->Id);
				$map->save($lkcategory);		
				
			}
		}
		$this->_category = $category;
	}
	
	public static function getTrainingPaging($filter,$limit=null,$pagingst=0) {
		$mapper = new Core_Data_Mappers_Training();
		$trainingCollections = $mapper->fetchAll($filter,$limit,$pagingst);
		if (isset($trainingCollections)&& null!==$trainingCollections)
			return $trainingCollections;
		else
			return array();
	}

 	public static function getTraining($filter) {
		$mapper = new Core_Data_Mappers_Training();
		$trainingCollections = $mapper->fetchAll($filter);
		if (isset($trainingCollections)&& null!==$trainingCollections)
			return $trainingCollections;
		else
			return array();
	}
	
 	public static function getTrainingIterator($filter,$nbelement=5) {
		$mapper = new Core_Data_Mappers_Training();
		$paginator= new Core_Common_Paginator($mapper,$filter);
		return $paginator;
	}
	
	/***
	 * return all training sessions available
	 * @return Core_Business_Trainingession|null return an collection of trainingsession
	 */
	public function getSessions() {
		return Core_Business_Trainingession::getSessionByTrainingId($this->_id);
	}
	
	public function save() {
		$map = new Core_Data_Mappers_Training();
		$map->save($this);
		$this->updateCategory();
		$this->updateZone();
	}
	
	protected function updateCategory() {
			if($this->_category!==null) {
				$map = new Core_Data_Mappers_Trainingexpertise();
				$map->flushCategory($this->_id);
				if(is_array($this->_category)) {
					foreach ($this->_category as $id) {
						if($id instanceof Core_Business_Trainingexpertise) {
							$id->setTrainingid($this->_id);
							$map->save($id);
						}
						else {
							$lkcategory = new Core_Business_Trainingexpertise();
							$lkcategory->setExpertiseid($id);
							$lkcategory->setTrainingid($this->Id);
							$map->save($lkcategory);
						}		
					}
				}
				else {
					$lkcategory = new Core_Business_Trainingexpertise();
					$lkcategory->setExpertiseid($this->_category);
					$lkcategory->setTrainingid($this->Id);
					$map->save($lkcategory);		
				}
			}
	}
	
	public static function getById($id) {
		$map = new Core_Data_Mappers_Training();
		return $map->find($id);
	}
	
	public function getAllZone() {
		$map = new Core_Data_Mappers_Trainingzone();
		$collection = array();
		$collection = $map->fetchAll(array("trainingid = ?"=>$this->_id));
		
		return $collection;
	}
	
	public function setZone($collection) {
		if($this->_id) {
				$map = new Core_Data_Mappers_Trainingzone();
				$map->flushtrainingzone($this->_id);
				foreach ($collection as $zone) {
					$trzone = new Core_Business_Trainingzone();
					$trzone->setTrainingid($this->_id);
					$trzone->setCodeinsee($zone->Insee);
					$map->save($trzone);
				}
		}
		$this->_zone = $collection;
	}
	protected function updateZone() {
		if($this->_zone!=null) {
			
			$map = new Core_Data_Mappers_Trainingzone();
			$map->flushtrainingzone($this->_id);
			
			foreach ($this->_zone as $zone) {
				$z = Core_Business_Zone::getByCodeinsee($zone);
				$z = current($z);
				$trzone = new Core_Business_Trainingzone();
				$trzone->setTrainingid($this->_id);
				$trzone->setCodeinsee($z->Insee);
				$map->save($trzone);
			}
		}
	}
	public function hasTrainer() {
		$map = new Core_Data_Mappers_Trainingcandidate();
		$query = $map->getDbTable()->Select()
				->where("trainingid = ?",$this->Id)
				->where("choose = ?",1);
		$row = $map->fetchQuery($query);
		if(isset($row) && count($row)>0)
			return true;
		else 
			return false;
	}
}
?>